package cn.com.czpb.client.module.personal.acticity;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import cn.com.czpb.client.R;
import cn.com.czpb.client.api.InterfaceJsonfile;
import cn.com.czpb.client.app.MToolBarActivity;
import cn.com.czpb.client.custorm.smartrefreshalayout.ClassHeader_other;
import cn.com.czpb.client.model.usercenter.HistoryDataEntity;
import cn.com.czpb.client.model.usercenter.HistoryEntity;
import cn.com.czpb.client.module.personal.adapter.HistoryDayAdapter;
import cn.com.czpb.client.net.Factory;
import cn.com.czpb.client.utils.DeviceUtils;
import cn.com.czpb.client.utils.LogUtils;
import cn.com.czpb.client.utils.ParamUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

public class HistoryActivity extends MToolBarActivity implements OnRefreshListener, BaseQuickAdapter.OnItemChildClickListener,BaseQuickAdapter.RequestLoadMoreListener{
    @BindView(R.id.swipe_layout)
    SmartRefreshLayout swipe_layout;
    @BindView(R.id.tv_empty_collection_activity)
    TextView tv_empty_collotion_activity;
    @BindView(R.id.layout_title)
    TextView tv_title;
    @BindView(R.id.ll_back)
    LinearLayout ll_back;

    @BindView(R.id.ptrlistview_activity_collection)
    RecyclerView ptrlistview;
    private HistoryDayAdapter adapter;
    private ArrayList<HistoryDataEntity> list;
    private int Page = 1;
    private static final int PageSize = 15;
    private boolean mFlagRefresh = true;//刷新还是加载


    @Override
    protected int setLayoutId() {
        return R.layout.activity_history;
    }
    @Override
    public void initData() {
        super.initData();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        tv_title.setText("浏览历史");
        init();
    }

    private void init() {
        ll_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });

        list = new ArrayList<>();
        getHistoryForServer();
        adapter = new HistoryDayAdapter(list,activity);
        GridLayoutManager manager = new GridLayoutManager(activity, 1);

        ptrlistview.setLayoutManager(manager);
        ptrlistview.setAdapter(adapter);


        adapter.setOnItemChildClickListener(this);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                LogUtils.e("onItemClick--" + position);
                
            }
        });
//        swipe_layout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
//            @Override
//            public void onRefresh() {
//                mFlagRefresh=true;
//                Page=1;
//                list.clear();
//                getHistoryForServer();
//            }
//        });

        swipe_layout.setRefreshHeader(new ClassHeader_other(this));
        swipe_layout.setOnRefreshListener(this );
        adapter.setOnLoadMoreListener(this, ptrlistview);
    }



    private void getHistoryForServer() {
        Map<String, String> map = new HashMap<>();

        map.put("siteid", InterfaceJsonfile.SITEID);
        map.put("uid", spu.getUser().getUid());
        map.put("device", DeviceUtils.getMyUUID(activity));
        Factory.provideHttpService().history(ParamUtils.getRequestParam(map))
                .onBackpressureBuffer()
                .subscribeOn(Schedulers.io())
                .filter(new Func1<HistoryEntity, Boolean>() {
                    @Override
                    public Boolean call(HistoryEntity historyEntity) {
                        return true;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<HistoryEntity>() {
                    @Override
                    public void call(HistoryEntity historyEntity) {
                        LogUtils.e("code--" + historyEntity.code);
                        if (mFlagRefresh){
                            swipe_layout.finishRefresh();

                        }
                        if ("200".equals(historyEntity.code)) {

                            setListData(historyEntity.data);
                            adapter.notifyDataSetChanged();
                            LogUtils.e(list.size() + "");
                        } else if ("209".equals(historyEntity.code)) {

                            list.clear();
                            adapter.notifyDataSetChanged();
                        } else {
                            if (!mFlagRefresh) {
                                Page--;
                            }
                        }
                        mFlagRefresh = false;
                    }

                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        LogUtils.i("throwable--" + throwable.getMessage());
                    }
                });


    }
    public void setListData(List<HistoryDataEntity> list) {
        if (mFlagRefresh) {
            adapter.setNewData(list);
            adapter.notifyDataSetChanged();
        } else {
            adapter.addData(list);
            adapter.notifyDataSetChanged();
            adapter.loadMoreComplete();
        }
        LogUtils.i("size-->" + adapter.getData().size());
        if (list.size() < PageSize) {
            //关闭刷新
            adapter.loadMoreEnd(true);
        }
    }





    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {

    }

    @Override
    public void onLoadMoreRequested() {
        Page++;
        getHistoryForServer();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }



    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        refreshlayout.getLayout().postDelayed(new Runnable() {
            @Override
            public void run() {
                mFlagRefresh = true;
                Page = 1;
                getHistoryForServer();
            }
        },2000);
    }
}
